package com.rongshu.view.userInfo.presenter;

import android.content.Context;

import com.rongshu.R;
import com.rongshu.api.RetorfitService;
import com.rongshu.view.base.mvp.BasePresenter;
import com.rongshu.view.base.mvp.INetBean;
import com.rongshu.view.discovery.bean.AuthorDetailBean;
import com.rongshu.view.discovery.bean.SearchAuthorResponse;
import com.rongshu.view.userInfo.iview.GiveLikeUserView;
import com.widget.toast.ToastUtils;

import java.util.List;

import rx.Subscriber;
import rx.functions.Func1;

public class GiveLikeUserPresenter extends BasePresenter<GiveLikeUserView> {

    public GiveLikeUserPresenter(Context context) {
        super(context);
    }

    /**
     * 文章点赞用户列表
     */
    public void getLikeUserList(int user_id, int contribute_id, int pageIndex, int pageSize, String sign) {
        RetorfitService.getLikeUserList(user_id, contribute_id, pageIndex, pageSize, sign)
                .map(new Func1<SearchAuthorResponse, List<AuthorDetailBean>>() {
                    @Override
                    public List<AuthorDetailBean> call(SearchAuthorResponse response) {
                        List<AuthorDetailBean> list = response.getResponse();
                        for (AuthorDetailBean bean : list) {
                            bean.setUser_id_to(bean.getUser_id());
                        }
                        return list;
                    }
                })
                .compose(getView().<List<AuthorDetailBean>>bindToLife())
                .subscribe(new Subscriber<List<AuthorDetailBean>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(List<AuthorDetailBean> beanList) {
                        if (getView() != null) {
                            getView().onLikeUserList(beanList);
                        }
                    }
                });
    }

    /**
     * 取消关注
     */
    public void cancelConcernWith(int user_id, int contribute_id, int user_id_to, String sign) {
        RetorfitService.cancelConcernWith(user_id, contribute_id, user_id_to, sign)
                .subscribe(new Subscriber<INetBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(INetBean iNetBean) {
                        ToastUtils.show(mContext.getString(R.string.cancel_attention_success));
                        if (getView() != null) {
                            getView().onConcernStatus(iNetBean,false);
                        }
                    }
                });
    }

    /**
     * 关注
     */
    public void concernWith(int user_id, int contribute_id, int user_id_to, String sign){
        RetorfitService.ConcernWith(user_id, contribute_id, user_id_to, sign)
                .subscribe(new Subscriber<INetBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(INetBean iNetBean) {
                        ToastUtils.show(mContext.getString(R.string.add_attention_success));
                        if (getView() != null) {
                            getView().onConcernStatus(iNetBean,true);
                        }
                    }
                });
    }
}
